//
//  MonitorRealTimeViewController.m
//  Light
//
//  Created by 石雄伟 on 15/12/19.
//  Copyright © 2015年 骁唐. All rights reserved.
//

#import "MonitorRealTimeViewController.h"

@interface MonitorRealTimeViewController ()
//心形图标
@property (weak, nonatomic) IBOutlet UIButton *buttonHeart;//不可以点击，只做心跳的展示，选中和未选中
//背景 心脏/脑的图标   bgjd
@property (weak,nonatomic) IBOutlet UIImageView * imageViewHeartOrHead;
//UIView 画线
@property (weak,nonatomic) IBOutlet UIView * viewWare;
//瞬时心跳
@property (weak,nonatomic) IBOutlet UILabel * heartOnTime;
//平均心跳
@property (weak,nonatomic) IBOutlet UILabel * heartAverage;
//心形图标距离右边的编剧
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *LayoutConstraintRight;

//数据(展示没有利用到，计算平均心态，顺时心跳的时候需要利用)
@property (strong,nonatomic) NSMutableArray *mutableArrayData;
//画板
@property (strong,nonatomic) WaveView * waveView;

//平均心率是否设置白色
@property (nonatomic,assign) NSInteger showWhite;
//显示的String平均心率
@property (nonatomic,assign) NSString * showString;

@end

@implementation MonitorRealTimeViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    delegate.isAtHeart = YES;//处于心电界面
    
    if ([delegate.type isEqualToString:@"1"])
    {//如果是脑电图
        self.buttonHeart.hidden = YES;//心形button 隐藏
        self.imageViewHeartOrHead.image = [UIImage imageNamed:@"bgnd"];//大脑底图
        //参数相关设置默认数据
        //第一行参数
        self.heartAverage.text = @"alpha：-- uv    theta：-- uv    注意力：-- ";
        //第二行参数
        self.heartOnTime.text = @"beta：--  uv    gamma：-- uv    放松度：-- ";
        
    }
    else if ([delegate.type isEqualToString:@"2"])
    {//如果是心电图
        self.buttonHeart.selected = !self.buttonHeart.selected;//设置为白色的图标
        self.LayoutConstraintRight.constant  -= 20;
        //平均心率：110 bpm    HRV：123   ms
        //之前的 平均心率：-- bpm
        self.heartAverage.text = @"平均心率：-- bpm    HRV：--   ms";
        //瞬时心率：110 bpm    呼吸：123 Tpm
        //之前的  瞬时心率：-- bpm
        self.heartOnTime.text = @"瞬时心率：-- bpm    呼吸：-- Tpm";
        self.imageViewHeartOrHead.image = [UIImage imageNamed:@"bgxd"];//心脏底图
        
    }
    else if ([delegate.type isEqualToString:@"3"])
    {//如果是肌电图，需要计算肌肉强度、肌肉疲劳度 2个参数指标
        self.buttonHeart.hidden = YES;//心形button 隐藏
        self.imageViewHeartOrHead.image = [UIImage imageNamed:@"bgjd"];//肌肉底图
        //参数相关设置默认数据
        //第一行参数
        self.heartAverage.text = @"肌肉强度：--   mv";
        //第二行参数
        self.heartOnTime.text = @"肌肉疲劳度：-- Hz";
    }
    else
    {
        //其他未知错误进入的
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //旋转屏幕，但是只旋转当前的View
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    self.view.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);
    [self configUI];//设置返回按钮
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabelTemp:) name:_NOTIFICATION_UPDATE_LABEL object:nil];//创建一个通知。用来刷新画图板
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];//隐藏状态栏
    self.showWhite = 0;//第一次初始化
}

//状态栏样式
- (UIStatusBarStyle)preferredStatusBarStyle
{//白色
    return UIStatusBarStyleLightContent;
}

//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark -
#pragma mark 配置返回按钮
- (void)configUI
{
    //注意，初始化frame的时候，需要注意。屏幕宽高，不用调转。之前屏幕显示不全，就是这个地方。现在是全屏显示
//    self.waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];//原来
    self.waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH)];
    self.waveView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.waveView];//添加到view上。
    //返回按钮
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(25,15, 60, 30)];
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    backImage.image = [UIImage imageNamed:@"left"];
    UILabel *backlLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 40, 30)];
    backlLabel.text = @"返回";
    backlLabel.textColor = [UIColor whiteColor];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 140)];

    [backBtn addTarget:self action:@selector(backClickB) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backClickB)];
    [backView addGestureRecognizer:click];
    [backView addSubview:backImage];
    [backView addSubview:backlLabel];
    [backView addSubview:backBtn];
    backView.backgroundColor = [UIColor clearColor];
    [self.waveView addSubview:backView];//添加返回按钮
}

#pragma mark -
#pragma mark 返回上级选择界面
-(void) backClickB
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    delegate.scope = -100;
    delegate.isHeading = NO;
    delegate.isHearting = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 更新label展示的信息
- (void)updateLabelTemp:(NSNotification *)dictionaryTemp{
    //判断具体是哪种，然后分别取值，然后进行信息排列优化后展示
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    //以下是计算参数指标，画图在上面的主线程中。肌电需要先对数据行滤波处理。
    if ([delegate.type isEqualToString:@"1"])
    {//如果是脑电图，需要计算alpha、theta、注意力、beta、gamma、放松度 6个参数指标
        [self updateLabelHead:dictionaryTemp];
    }
    else if ([delegate.type isEqualToString:@"2"])
    {//如果是心电图，需要计算平均（瞬时）心率、HRV、呼吸 4个参数指标
        [self updateLabelHeart:dictionaryTemp];
    }
    else if ([delegate.type isEqualToString:@"3"])
    {//如果是肌电图，需要计算肌肉强度、肌肉疲劳度 2个参数指标
        [self updateLabelMuscle:dictionaryTemp];
    }
    else
    {
        //其他未知错误进入的
    }
}

#pragma mark -
#pragma mark 更新label展示的信息（脑电界面）
- (void)updateLabelHead:(NSNotification *)dictionaryHeart
{//注意这边需要利用label设置不同的颜色，数字部分红色字体，字号大一些,其他文字无要求
    TJJLog(@"更新label展示的脑电参数信息");
    //添加 字典，脑电的6个参数的值通过key值设置传递
    NSNumber * headAlpha = (NSNumber *)dictionaryHeart.userInfo[@"alpha"];//alpha
    NSNumber * headTheta= (NSNumber *)dictionaryHeart.userInfo[@"theta"];//theta
    NSNumber * headAttention = (NSNumber *)dictionaryHeart.userInfo[@"attention"];//注意力
    NSNumber * headBeta= (NSNumber *)dictionaryHeart.userInfo[@"beta"];//beta
    NSNumber * headGamma = (NSNumber *)dictionaryHeart.userInfo[@"gamma"];//gamma
    NSNumber * headRelaxation= (NSNumber *)dictionaryHeart.userInfo[@"relaxation"];//放松度
    
    NSMutableString * stringUpTemp = [[NSMutableString alloc] initWithCapacity:0];//第一行 文字展示
    NSMutableString * stringDownTemp = [[NSMutableString alloc] initWithCapacity:0];//第二行 文字展示
    BOOL isWhiteHeadAlpha;//alpha值是否是白色字体
    BOOL isWhiteHeadTheta;//atheta值是否是白色字体
    BOOL isWhiteHeadAttention;//注意力值是否是白色字体
    BOOL isWhiteHeadBeta;//beta值是否是白色字体
    BOOL isWhiteHeadGamma;//gamma值是否是白色字体
    BOOL isWhiteHeadRelaxation;//放松度值是否是白色字体
    
    //参数相关设置默认数据
    [stringUpTemp appendString:[NSString stringWithFormat:@"alpha：%.1f uv",[headAlpha floatValue]]];
    NSInteger lengthAlpha = stringUpTemp.length;//第一行第一个参数的长度
    isWhiteHeadAlpha = NO;//第一个参数不是白色，白色为“--”
    
    [stringUpTemp appendString:[NSString stringWithFormat:@"    theta：%.1f uv",[headTheta floatValue]]];
    NSInteger lengthTheta = stringUpTemp.length-lengthAlpha;//第一行第二个参数的长度（包括最前面的4个空格）
    isWhiteHeadTheta = NO;
    
    [stringUpTemp appendString:[NSString stringWithFormat:@"    注意力：%.1f",[headAttention floatValue]]];
    NSInteger lengthAttention = stringUpTemp.length-lengthAlpha-lengthTheta;//第一行第二个参数的长度（包括最前面的4个空格）
    isWhiteHeadAttention = NO;
    
    NSInteger lengthUp = stringUpTemp.length;//第一行 总长度

    
    //第二行参数赋值
    [stringDownTemp appendString:[NSString stringWithFormat:@"beta：%.1f uv",[headBeta floatValue]]];
    NSInteger lengthBeta = stringDownTemp.length;//第二行第一个参数的长度
    isWhiteHeadBeta = NO;//第二行第一个参数不是白色，白色为“--”
    
    [stringDownTemp appendString:[NSString stringWithFormat:@"    gamma：%.1f uv",[headGamma floatValue]]];
    NSInteger lengthGamma = stringDownTemp.length-lengthBeta;//第二行第二个参数的长度（包括最前面的4个空格）
    isWhiteHeadGamma = NO;//第二行第二个参数不是白色，白色为“--”
    
    [stringDownTemp appendString:[NSString stringWithFormat:@"    放松度：%.1f",[headRelaxation floatValue]]];
    NSInteger lengthRelaxation= stringDownTemp.length-lengthBeta-lengthGamma;//第二行第二个参数的长度（包括最前面的4个空格）
    isWhiteHeadRelaxation = NO;//第二行第三个参数不是白色，白色为“--”
    
    NSInteger lengthDown = stringDownTemp.length;//第二行 总长度
    
    NSMutableAttributedString * stringUp = [[NSMutableAttributedString alloc]initWithString:stringUpTemp];//设置第一行文字
    [stringUp addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, lengthUp)];//设置前面文字白色
    [stringUp addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, lengthUp)];//设置前面文字大小
    
    if (!isWhiteHeadAlpha)
    {//Alpha数值 设置红色字体
        [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, lengthAlpha-9)];//设置中间数字红色
    }
    
    if (!isWhiteHeadTheta)
    {//Theta数值 设置红色字体
        [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthAlpha+10, lengthTheta-13)];//设置中间数字红色
    }
    
    if (!isWhiteHeadAttention)
    {//Attention(注意力)数值 设置红色字体
        [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthUp-lengthAttention+8, lengthAttention-8)];//设置中间数字红色
    }
    
    
    
    NSMutableAttributedString* stringDown = [[NSMutableAttributedString alloc]initWithString:stringDownTemp];//设置第二行文字
    [stringDown addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, lengthDown)];//设置前面文字白色
    [stringDown addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, lengthDown)];//设置前面文字大小
    if (!isWhiteHeadBeta)
    {//Alpha数值 设置红色字体
        [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, lengthBeta-8)];//设置中间数字红色
    }
    
    if (!isWhiteHeadGamma)
    {//Theta数值 设置红色字体
        [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthBeta+10, lengthGamma-13)];//设置中间数字红色
    }
    
    if (!isWhiteHeadRelaxation)
    {//Attention(注意力)数值 设置红色字体
        [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthDown-lengthRelaxation+8, lengthRelaxation-8)];//设置中间数字红色
    }
    
    
    dispatch_async(dispatch_get_main_queue(), ^{//主线程  刷新UI
        self.heartAverage.attributedText = stringUp;//添加第一行
        self.heartOnTime.attributedText = stringDown;//添加第二行
        AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
        delegate.isHeading = NO;//标记脑电参数计算完毕
    });
}


#pragma mark -
#pragma mark 更新label展示的信息（肌电界面）
- (void)updateLabelMuscle:(NSNotification *)dictionaryHeart
{//注意这边需要利用label设置不同的颜色，数字部分红色字体，字号大一些,其他文字无要求
    TJJLog(@"更新label展示的肌电信息");
    //添加 字典，将平均心率和瞬时心率的值通过key值设置传递
    NSNumber * muscleFatigue = (NSNumber *)dictionaryHeart.userInfo[@"fatigue"];
    NSNumber * muscleStrength= (NSNumber *)dictionaryHeart.userInfo[@"strength"];
    NSString * stringMuscleFatigue;//肌肉疲劳度文字展示
    NSString * stringMuscleStrength;//肌肉强度文字展示
    BOOL isWhiteMuscleFatigue;//肌肉疲劳度是否是白色字体
    BOOL isWhiteMuscleStrength;//肌肉强度是否是白色字体
    stringMuscleFatigue = [NSString stringWithFormat:@"肌肉疲劳度：%4lu Hz",[muscleFatigue integerValue]];
    isWhiteMuscleFatigue = NO;

    stringMuscleStrength = [NSString stringWithFormat:@"肌肉强度：%4.1f mv",[muscleStrength floatValue]];
    isWhiteMuscleStrength = NO;
    
    NSMutableAttributedString* stringUp = [[NSMutableAttributedString alloc]initWithString:stringMuscleStrength];//设置肌肉强度
    [stringUp addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, 5)];//设置前面文字白色
    [stringUp addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 5)];//设置前面文字大小
    
    NSMutableAttributedString* stringDown = [[NSMutableAttributedString alloc]initWithString:stringMuscleFatigue];//设置肌肉疲劳度
    [stringDown addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, 6)];//设置前面文字白色
    [stringDown addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 6)];//设置前面文字大小
    
    if (isWhiteMuscleStrength)
    {//肌肉强度显示白色字体
        [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(5, stringMuscleStrength.length-8)];//设置中间白色红色
    }
    else
    {//需要刷新 设置红色字体
        [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, stringMuscleStrength.length-8)];//设置中间数字红色
    }
    
    if (isWhiteMuscleFatigue)
    {//肌肉疲劳度 显示白色
        [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(6, stringMuscleFatigue.length-9)];//设置中间数字白色 5(前)+3（后）
    }
    else
    {//需要刷新 设置红色字体
        [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, stringMuscleFatigue.length-9)];//设置中间数字红色 5(前)+3（后）
    }
    
    [stringUp addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(5, stringMuscleStrength.length-8)];//设置中间数字大小
    [stringUp addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(stringMuscleStrength.length-3, 3)];//设置后面文字白色 bpm
    [stringUp addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(stringMuscleStrength.length-3, 3)];////设置前面文字大小 bpm
    
    [stringDown addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(6, stringMuscleFatigue.length-9)];//设置中间数字大小
    [stringDown addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(stringMuscleFatigue.length-3, 3)];//设置后面文字白色 bpm
    [stringDown addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(stringMuscleFatigue.length-3, 3)];////设置前面文字大小 bpm
    dispatch_async(dispatch_get_main_queue(), ^{//主线程  刷新UI
        self.heartAverage.attributedText = stringUp;//添加第一行
        self.heartOnTime.attributedText = stringDown;//添加第二行
    });
}


#pragma mark -
#pragma mark 更新label展示的平均心率信息（心电界面）
- (void)updateLabelHeart:(NSNotification *)dictionaryHeart
{//注意这边需要利用label设置不同的颜色，数字部分红色字体，字号大一些,其他文字无要求
    TJJLog(@"更新label展示的平均心率信息");
    //添加 字典，将平均心率和瞬时心率的值通过key值设置传递
    NSNumber * numberAverage = (NSNumber *)dictionaryHeart.userInfo[@"Average"];
    NSNumber * numberOntime= (NSNumber *)dictionaryHeart.userInfo[@"Ontime"];
    NSNumber * numberhrv = (NSNumber *)dictionaryHeart.userInfo[@"hrv"];
    NSNumber * numberresrespiration= (NSNumber *)dictionaryHeart.userInfo[@"resrespiration"];
    //    self.heartAverage.text = @"平均心率：-- bpm    HRV：--   ms";
    //    self.heartOnTime.text = @"瞬时心率：-- bpm    呼吸：-- Tpm";
    NSMutableString * stringAverage = [[NSMutableString alloc] initWithCapacity:0];//第一行
    NSMutableString * stringOntime = [[NSMutableString alloc] initWithCapacity:0];//第二行
    BOOL isUpdate;//时候闪烁 心跳图标
    BOOL isWhiteOntime;//瞬时心跳是否是白色字体
    BOOL isWhiteAverage;//平均心跳是否是白色字体
    BOOL isWhiteHRV;//HRV是否是白色字体
    BOOL isWhiteResrespiration;//呼吸是否是白色字体
    if ([numberOntime integerValue] > 160 || [numberOntime integerValue] < 40 )
    {//如果瞬时心率大于160，小于40
        [stringOntime appendString:@"瞬时心率：-- bpm"];
        isWhiteOntime = YES;//第一个参数是白色，白色为“--”
    }
    else
    {//如果瞬时心率数据正常
        [stringOntime appendString:[NSString stringWithFormat:@"瞬时心率：%lu bpm",[numberOntime integerValue]]];
        isWhiteOntime = NO;//第一个参数不是白色，白色为“--”
    }
    NSInteger lengthOntime = stringOntime.length;//第二行第一个参数的长度
    
    if([numberAverage integerValue] > 160 || [numberAverage integerValue] < 40)
    {//如果平均心率大于160，小于40
        [stringAverage appendString:@"平均心率：-- bpm"];
        isWhiteAverage = YES;//第一个参数是白色，白色为“--”
    }
    else
    {//如果平均心率数据正常
        [stringAverage appendString:[NSString stringWithFormat:@"平均心率：%lu bpm",[numberAverage integerValue]]];
        isWhiteAverage = NO;//第一个参数不是是白色，白色为“--”
    }
    NSInteger lengthAverage = stringAverage.length;//第一行第一个参数的长度
    
    
    if (([numberAverage integerValue] <= 160 && [numberAverage integerValue] >= 40) || ([numberOntime integerValue] <= 160 && [numberOntime integerValue] >= 40))
    {//如果平均心率和瞬时心率，只要有一个是正常状态，就闪烁心跳图标。
        isUpdate = YES;
    }
    else
    {
        isUpdate = NO;
    }
    
    //    if ([numberhrv floatValue] < 0 || [numberhrv floatValue] > 100){
    //        //如果HRV 不在 0—100 这个区间范围内
    //        [stringAverage appendString:@"    HRV：-- ms"];
    //    } else{
    [stringAverage appendString:[NSString stringWithFormat:@"    HRV：%2.1f ms",[numberhrv floatValue]]];
    NSInteger lengthAverageSum = stringAverage.length;//第一行总长度
    NSInteger lengthHRV = lengthAverageSum - lengthAverage;//第一行第二个参数的长度
    isWhiteHRV = NO;
    
    //    }
    
    //    if ([numberresrespiration floatValue] < 0 || [numberresrespiration floatValue] > 100){
    //        //如果呼吸 不在 0—100 这个区间范围内
    //        [stringOntime appendString:@"    呼吸：-- Hz"];
    //    } else{
    [stringOntime appendString:[NSString stringWithFormat:@"    呼吸：%2.1f Tpm",[numberresrespiration floatValue]]];
    NSInteger lengthOntimeSum = stringOntime.length;//第二行总长度
    NSInteger lengthResrespiration = lengthOntimeSum - lengthOntime;//第二行第二个参数的长度
    isWhiteResrespiration = NO;
    //    }
    
    
    
    NSMutableAttributedString* stringAverageAtt = [[NSMutableAttributedString alloc]initWithString:stringAverage];//设置第一行文字
    [stringAverageAtt addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, lengthAverageSum)];//设置第一行所有文字白色
    [stringAverageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, lengthAverageSum)];//设置所有文字大小
    
    if (!isWhiteAverage)
    {//平均心率显示白色字体 第一行 第一个参数
        [stringAverageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, lengthAverage-9)];//设置中间数字红色
    }
    
    if (!isWhiteHRV)
    {//HRV 数值 设置红色字体  第一行 第二个参数
        [stringAverageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthAverage+8, lengthHRV-11)];//设置中间数字红色
    }
    
    NSMutableAttributedString* stringOntimeAtt = [[NSMutableAttributedString alloc]initWithString:stringOntime];//设置第二行文字
    [stringOntimeAtt addAttribute:NSForegroundColorAttributeName value:[[UIColor whiteColor] colorWithAlphaComponent:.87f] range:NSMakeRange(0, lengthOntimeSum)];//设置第二行所有文字白色
    [stringOntimeAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, lengthOntimeSum)];//设置前面文字大小
    
    if (!isWhiteOntime)
    {
        [stringOntimeAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, lengthOntime-9)];
    }
    
    if (!isWhiteResrespiration)
    {
        [stringOntimeAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(lengthOntime+7, lengthResrespiration-11)];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{//主线程  刷新UI
        self.heartAverage.attributedText = stringAverageAtt;
        self.heartOnTime.attributedText = stringOntimeAtt;
        self.LayoutConstraintRight.constant  = 290;
        if (isUpdate)
        {
            self.buttonHeart.selected = !self.buttonHeart.selected;
        }
        else
        {
            self.buttonHeart.selected = YES;//心形设置为白色的
        }
        
        AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
        delegate.isHearting = NO;
    });
}


#pragma mark -
#pragma mark 销毁
-(void)dealloc
{
    [self.waveView.addDataTimer invalidate]; //释放
    self.waveView.addDataTimer = nil;
    self.waveView = nil;
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    delegate.isHeading = NO;
    delegate.isHearting = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
